package com.example.administrator.myapplication;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;

/*
 通知的使用
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private NotificationManager mNoteManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //点击事件
        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
        findViewById(R.id.button3).setOnClickListener(this);
        findViewById(R.id.button4).setOnClickListener(this);
        findViewById(R.id.button5).setOnClickListener(this);
        //得到通知管理类
        mNoteManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button1:
                testNote();
                break;
            case R.id.button2:
                updateNote();
                break;
            case R.id.button3:
                testBigView();
                break;
            case R.id.button4:
                testMyNote();
                break;
            case R.id.button5:
                testProgress();
                break;

        }
    }
    //进度条通知
    private void testProgress() {
        final NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setTicker("您有一条更新的新消息！").setContentTitle("通知标题");
        //必备
        builder.setSmallIcon(R.mipmap.ic_launcher);
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 100 ; i++) {
                    builder.setProgress(100,i,false);
                    builder.setContentText(i + "%");
                    mNoteManager.notify(103, builder.build());
                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                //下载完成之后
                builder.setContentText("下载完成");
                builder.setProgress(0,0,true);
                mNoteManager.notify(103,builder.build());
            }
        }).start();
    }

    private void testMyNote() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setTicker("您有一条更新的新消息！");
        //必备
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setAutoCancel(true);
        //创建自定义视图
        RemoteViews remoteView = new RemoteViews(getPackageName(),R.layout.my_note_item);
        remoteView.setTextColor(R.id.button, Color.RED);
        //添加事件
        Intent intent = new Intent(this,SecendActivity.class);
        intent.putExtra("msg", "我是自定义的通知");
        PendingIntent pIntent = PendingIntent.getActivity(this, 10, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        //为自定义通知中的某个空间设置点击事件
//        remoteView.setOnClickPendingIntent(R.id.button, pIntent);

        builder.setContent(remoteView);
        //整个通知的点击事件
        builder.setContentIntent(pIntent);
        //构造消息
        Notification note = builder.build();
        mNoteManager.notify(102, note);
    }

    private void testBigView() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setTicker("您有一条更新的新消息！");
        builder.setContentTitle("更新通知的通知！").setContentText("消息的内容").setContentInfo("info");
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        builder.setSmallIcon(R.mipmap.ic_launcher);

        //大视图
        NotificationCompat.InboxStyle iStyle = new NotificationCompat.InboxStyle();
        iStyle.setBigContentTitle("大视图标题");
        iStyle.setSummaryText("大视图的Summary");
        //添加大视图的内容
        for (int i = 0; i < 5; i++)
        {
            iStyle.addLine("我是内容"+i);
        }
        builder.setStyle(iStyle);

        //构造消息
        Notification not = builder.build();
        mNoteManager.notify(101, not);
    }

    //取消/更新通知
    private void updateNote() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        builder.setTicker("您有一条新消息！");
        builder.setContentTitle("我是标题").setContentText("消息内容").setContentInfo("info");
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        builder.setSmallIcon(R.mipmap.ic_launcher);
        builder.setNumber(100);
//        builder.setAutoCancel(true);
        //是否可取消
        builder.setOngoing(false);
        //设置事件
        /*
         * 两个PendingIntent对等：
         * requestCode要相同
         * intent的动作，类目，data必须相同，但是携带的数据可以不同
         */
        Intent intent = new Intent(this, SecendActivity.class);
        intent.putExtra("msg", "我是更新后的通知");
        PendingIntent pIntent = PendingIntent.getActivity(this, 10, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pIntent);

        //构造消息
        Notification not = builder.build();
        //发送通知
        /*
         * 通知的id
         * 通知
         */
        mNoteManager.notify(100, not);
    }

    private void testNote() {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        //构造一条通知
        //设置消息弹出来时的文本
        builder.setTicker("您有一条新消息！");
        /*
         * 消息的标题
         * 消息的内容
         * 消息的info
         */
        builder.setContentTitle("我是标题").setContentText("消息内容").setContentInfo("info");
        /*
         消息的大图标，小图标
         */
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
        builder.setSmallIcon(R.mipmap.ic_launcher);
        //如果没有设置ContentInfo,它会显示在ContentInfo的位置
        builder.setNumber(100);
        //用户点击后自动取消
//        builder.setAutoCancel(true);
        //是否可取消
        builder.setOngoing(false);
        //设置事件
        Intent intent = new Intent(this, SecendActivity.class);
        intent.putExtra("msg", "msg_______msg");
        /*
         * PendingIntent：封装一个延迟的intent对象。只有点击了通知栏之后才会执行此
         * intent flags:
         * PendingIntent.FLAG_CANCEL_CURRENT:表示创建方式为取消当前的，再创建新的
         * PendingIntent.FLAG_NO_CREATE:表示不会创建新的，只沿用以前的
         * PendingIntent.FLAG_ONE_SHOT:表示创建的intent只能使用一次
         * PendingIntent.FLAG_UPDATE_CURRENT:表示沿用并更新之前的
         */
        PendingIntent pIntent = PendingIntent.getActivity(this, 10, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pIntent);

        //构造消息
        Notification not = builder.build();
        //发送通知
        /*
         * 通知的id
         * 通知
         */
        mNoteManager.notify(100, not);

    }
}
